import datetime
import os

import numpy as np

import pyqtgraph as pg

win = pg.GraphicsLayoutWidget(show=True)
win.setBackground('white')

m = win.addPlot()
def plot(csv_path,x_index=0,y_index=2):
    csv_file = open(csv_path, 'r',encoding='utf-8')
    data = csv_file.read()
    data_x = []
    data_y = []
    start_time=0
    for n in range(len(data.split('\n'))):
        line = data.split('\n')[n].split(',')
        if n != 0 and line != ['']:
            if start_time==0:
                start_time=float(line[x_index])
            data_x.append(float(line[x_index])-start_time)
            data_y.append(float(line[y_index]))
    m.plot(x=data_x, y=data_y, symbolPen='w', title="yuan de test plot")
    axis_list = ['时间', '间隔', '延时', '收到包数量', '丢包率', '环境rssi', '数据rssi']
    m.setLabel('left', axis_list[y_index])  # 设置 Y 轴标签
    m.setLabel('bottom',axis_list[x_index])

if __name__ == '__main__':
    # csv_path='亿佰特230_1721799572.0941083.csv'
    # csv_path='亿佰特230_1721799572.0941083.csv'

    #x轴数据是csv文件的第一列时间 所以0 要绘制其他数据改变xy_index即可
    csv_name=sorted(os.listdir('test1'))[-1]
    #
    def read_time(csv_name):
        timestamp = float(csv_name[csv_name.find('_') + 1:csv_name.find('.')])
        print(timestamp)
        # 将时间戳转换为 datetime 对象
        dt_object = datetime.datetime.fromtimestamp(timestamp)

        # 格式化 datetime 对象为 "年-月-日 时:分:秒" 字符串
        date_string = dt_object.strftime("%Y-%m-%d_%H:%M:%S")

        print(date_string)  # 输出示例：2021-08-02 00:00:00
        print('-' * 50)
    #
    #
    # # print(os.path.join('test1',csv_name))
    # for csv_name1 in csv_name:
    #     read_time(csv_name1)
    plot(os.path.join('test1',csv_name),x_index=0,y_index=4)
    # ['时间', '间隔', '延时', '收到包数量', '丢包率', '环境rssi', '数据rssi']

    pg.exec()
